function deleteRole(ids, message) {
    bootbox.confirm({
        message: message,
        buttons: {
            confirm: {
                label: "确认",
                className: "btn-primary btn-sm",
            },
            cancel: {
                label: "取消",
                className: "btn-sm",
            }
        },
        callback: function (result) {
            if (result) {
                jQuery.ajax({
                    'url': root + "/role/delete.do",
                    'traditional': true,
                    'data': {
                        ids: ids
                    },
                    'dataType': 'text',
                    'type': 'post',
                    'success': function (rs) {
                        var back_url = root + "/role/list.do";
                        toastMessage(rs, back_url)
                    },
                    'error': function () {
                        jQuery("#taskDetail").hideLoading();
                        bootbox.dialog({
                            message: "系统繁忙，请稍后重试。",
                            buttons: {
                                "success": {
                                    "label": "确定",
                                    "className": "btn-sm btn-primary"
                                }
                            }
                        });
                    },
                    'async': true
                });
            }
        }
    });
}

$(function () {
    $("#addBtn").bind("click", function () {
        window.location.href = root + "/role/input.do";
    });

    var ids = "";
    $("#deleteBtn").click(function () {
        ids = "";
        $('table tr > td:first-child input:checkbox').each(function () {
            if ($(this).prop("checked") === true) {
                ids += $.trim($(this).closest("tr").children('td').eq(1).html()) + ",";
            }
        });

        if (ids === "") {
            $().toastmessage('showToast', {
                text: "请选择要删除的角色！",
                stayTime: 3000,
                sticky: false,
                position: 'top-center',
                type: 'warning'
            });
            return;
        }

        ids = ids.substring(0, ids.length - 1);
        deleteRole(ids, "确认要删除这些角色吗？")
    });

    $(document).on('click', 'th input:checkbox', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox').each(function () {
            // this.checked = that.checked;
            $(this).prop("checked", $(that).prop("checked"));
            var checked = $(this).prop("checked");
            if (checked === true)
                $(this).closest('tr').addClass('selected');
            else
                $(this).closest('tr').removeClass('selected');
        });
    });

    $('table tr > td:first-child input:checkbox').each(function () {
        var checked = $(this).prop("checked");
        if (checked === true)
            $(this).closest('tr').addClass('selected');
        else
            $(this).closest('tr').removeClass('selected');
    });

    $('tr > td:first-child input:checkbox').click(function () {
        var checked = $(this).prop("checked");
        if (checked === true)
            $(this).closest('tr').addClass('selected');
        else
            $(this).closest('tr').removeClass('selected');
    });

})
;

var List = (function () {
    return {
        editEntity: function ($this) {
            var id = $($this).parents("tr").children('td').eq(1).html();
            window.location.href = root + "/role/input.do?id=" + id;
        },
        deleteEntity: function ($this) {
            var id = $($this).parents("tr").children('td').eq(1).html();
            deleteRole(id, "确认要删除此角色吗？")
        },
    }
})();
